package com.javasm.mb_store_api_final.sys_control.usercontrol.sys_user.controller;

import com.javasm.common.Parameter;
import com.javasm.common.R;
import com.javasm.mb_store_api_final.sys_control.usercontrol.sys_user.entity.SysUser;
import com.javasm.mb_store_api_final.sys_control.usercontrol.sys_user.service.SysUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author Lx
 * @version 0.1
 * @className UserControl
 * @date 2024/7/29 20:19
 * @description
 * @since jdk11
 */
@RestController
@RequestMapping("/user")
public class UserControl {

    @Resource
    SysUserService sysUserService;

    @GetMapping("/query")
    public R queryAll(){
        List<SysUser> sysUsers = sysUserService.queryAll();
        return R.ok(sysUsers);
    }

    @GetMapping("/queryBy")
    public R queryBy(SysUser user){
        List<SysUser> sysUser = sysUserService.queryBy(user);
        return R.ok(sysUser);
    }
    @PostMapping("/add")
    public R add(@RequestBody SysUser user){
        Parameter.check(user.getUserName(), user.getUserAccount(), user.getUserPassword());
        sysUserService.add(user);
        return R.ok(user);
    }

    @DeleteMapping("/delete/{userId}")
    public R delete(@PathVariable Integer userId){
        sysUserService.deleteById(userId);
        return R.ok();
    }

    @PostMapping("/update")
    public R update(@RequestBody SysUser user) {
        Integer userId = user.getUserId();
        sysUserService.update(userId, user);
        return R.ok(sysUserService.queryAll());
    }

    @GetMapping("/paramList")
    public R paramList() {
        Map<String, Set<String>> map = sysUserService.paramList();
        return R.ok(map);
    }

    @GetMapping("/getTatol")
    public R getTatol() {
        long count =  sysUserService.countall();
        return R.ok(count);
    }
}
